# 该代码已经合并到featureExtraction中
# librosa is a Python library for analyzing audio and music. It can be used to extract the data from the audio files we will see it later.
import librosa
import librosa.display
import numpy as np
import pandas as pd
def noise(data):
    noise_amp = 0.035*np.random.uniform()*np.amax(data)
    data = data + noise_amp*np.random.normal(size=data.shape[0])
    return data

def stretch(data):
    return librosa.effects.time_stretch(data, rate=0.8)

def shift(data):
    shift_range = int(np.random.uniform(low=-5, high = 5)*1000)
    return np.roll(data, shift_range)

def pitch(data, sampling_rate):
    # 调整音高，这里 pitch_factor 表示半音数，0.7 是一个示例值，可以根据需要修改
    pitch_factor = 0.7
    # 正确传递 sr 和 n_steps 这两个关键字参数
    return librosa.effects.pitch_shift(data, sr=sampling_rate, n_steps=pitch_factor)


import matplotlib.pyplot as plt
# to play the audio files
from IPython.display import Audio

# taking any example and checking for techniques.
data_path = pd.read_csv("../data/data_path.csv")
path = np.array(data_path.Path)[1]
data, sample_rate = librosa.load(path)

# 1. Simple Audio¶
plt.figure(figsize=(14,4))
librosa.display.waveshow(y=data, sr=sample_rate)
Audio(path)
# plt.show()

# 2. Noise Injection¶
x = noise(data)
plt.figure(figsize=(14,4))
librosa.display.waveshow(y=x, sr=sample_rate)
Audio(x, rate=sample_rate)

# 3. Stretching¶
x = stretch(data)
plt.figure(figsize=(14,4))
librosa.display.waveshow(y=x, sr=sample_rate)
Audio(x, rate=sample_rate)

# 4. Shifting¶
x = shift(data)
plt.figure(figsize=(14,4))
librosa.display.waveshow(y=x, sr=sample_rate)
Audio(x, rate=sample_rate)

# 5. Pitch¶
x = pitch(data, sample_rate)
plt.figure(figsize=(14,4))
librosa.display.waveshow(y=x, sr=sample_rate)
Audio(x, rate=sample_rate)